package com.wrial.valid;
/*
 * @Author  Wrial
 * @Date Created in 9:43 2020/5/11
 * @Description
 */

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

//第一个泛型是表示给那个注解，第二个是对什么类型字段的校验
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {


    private Set<Integer> set = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        // 拿出所有合法值放入set
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            set.add(val);
        }
    }

    /**
     *
     * @param integer 需要进行校验的值
     * @param constraintValidatorContext 上下文信息
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
